﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ECommerce.BLL.Member;
using ECommerce.Model.Member;
using ECommerce.Common;
using Jzy.Core.Common.Web;
using Jzy.Core.Common;
using Jzy.Core.Utility;
using Jzy.Model.Common;

namespace ECommerce.Web.Site
{
    public partial class MemberEdit : SitePage
    {
        private MemberBaseBO Member
        {
            get
            {
                return MemberBaseBO.Instance<MemberBaseBO>();
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            base.CheckLogin();
            if (!IsPostBack)
            {
                List<EnumInfo> list = ConvertUtil.ConvertEnum(typeof(Gender));
                drpGender.DataSource = list;
                drpGender.DataTextField = "Name";
                drpGender.DataValueField = "ID";
                drpGender.DataBind();

                DispInfo();
            }
            AddAttr();
        }

        private void AddAttr()
        {
            WebUtil.SetTimeBox(Birthday, false);
        }

        private void DispInfo()
        {
            MemberBaseInfo info = Member.GetInfo(SiteSession.MemberID);
            EntityUtil.SetControl(pnlMain, info);

            litMemberLoginName.Text = info.LoginName;
            txtPassword.Attributes.Add("value", info.Password);
            Password2.Attributes.Add("value", info.Password);
            hidPassword.Value = info.Password;
            this.drpGender.SelectedValue = info.Gender.ToString();
            if (info.Birthday != null)
            {
                Birthday.Text = info.Birthday.Value.ToString("yyyy-MM-dd");
            }
        }

        protected void btnSave_Click(object sender, ImageClickEventArgs e)
        {
            MemberBaseInfo info = this.Member.GetInfo(SiteSession.MemberID);
            EntityUtil.SetEntity(pnlMain, info);
            info.Gender = int.Parse(drpGender.SelectedValue);
           
            
            if (txtPassword.Text.Trim() != hidPassword.Value)
            {
                info.Password = txtPassword.Text;
                info.Password = this.Member.Encript(info);
            }
            
            this.Member.Save(info);
            DispInfo();
            JSUtil.Alert("保存成功");
        }
    }
}